package com.wxl.common.wiget

import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import com.wxl.common.util.Loog

/**
 * create file time : 2021/3/1
 * create user : wxl
 * subscribe :
 */

class ImageTextTabGroupLayout : LinearLayout {

    val vTabs = ArrayList<VTab>()
    lateinit var l : OnItemTabClickListener

    @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : super(
        context,
        attrs,
        defStyleAttr
    )

     fun parseVTabChild() {
        for (i in 0..childCount) {
            val childAt = getChildAt(i)
            if (childAt is VTab) {
                val tab: VTab = childAt
                tab.setOnClickListener {
                    notifyTabClickChanged(tab)
                }
                vTabs.add(tab)
            }
        }
    }


    fun click(position: Int){
        vTabs.takeIf { position<vTabs.size }?.let {
            for (i in 0.. it.size){
                if (position == i){
                    notifyTabClickChanged(it[i])
                    break
                }
            }
        }
    }


    private fun notifyTabClickChanged(tab: VTab){
        val position:Int = vTabs.indexOf(tab)
        for(tabx in vTabs){
            tabx.onClick(false)
        }
        tab.onClick(true)
        l.onItemTabClick(position,tab)
    }


    fun setOnItemTabClick(l:OnItemTabClickListener) {
        this.l = l
    }


    companion object {
        interface OnItemTabClickListener {
            fun onItemTabClick(position:Int,tab:VTab)
        }
    }

}